/**
 * Author: Joe
 * Date: 2018-03-26
 * Description: none
 */


function genTableActions(actions, iconSize) {
  actions = actions || [];
  iconSize = iconSize || '';
  var $line = $('<div class="table-action"></div>');

  var $item;
  actions.forEach(function(action) {
    if (action.disable) return;

    // // generate icon
    // $icon = [
    //   '<i class="fas ',
    //   getIconType(action.iconType),
    //   ' ',
    //   iconSize,
    //   '"></i>'
    // ].join('');
    // ''+staeMsg+'</a>'
    // generate item with icon
    $item = $(
      [
        '<a href="', action.actionUrl, '" gm-click="', action.actionName, '" class="table-btn">',
        action.title,
        '</a>'
      ].join('')
    );
    $item.css({
      color: action.color,
      marginLeft: '10px',
    });

    $line.append($item);
  });

  $line.css({
    display: '-webkit-flex',
    justifyContent: 'space-around',
    margin: '0 5%',
  });

  $line.find('span').css({
    cursor: 'pointer',
  });

  return String($line.html());
}

var formatList = [
  'docx',
  'dotx',
  'doc',
  'dot',
  'docm',
  'dotm',
  'pdf',
  'xps',
  'rtf',
  'txt',
  'xml',
  'odt',
  'wtf',
  'xlsx'
];
function formatScreen(val) {
  var formatStr = formatList.toString();
  if (formatStr.indexOf(val) === -1){
    return false;
  }
  return true;
}
// 解析、组合操作===============================================================
// 解析查询参数
function parseQuery(decode, hack) {
  var qs;
  if (hack) {
    qs = location.href.substring(location.href.indexOf('?') + 1);
  }
  else {
    qs = location.search.length > 0 ? location.search.substring(1) : '';
  }
  var args = {};
  var items = qs.length ? qs.split('&') : [];
  var name = null, value = null;
  items.forEach(function (item) {
    var itemPair = item.split('=');
    name = decode ? decodeURIComponent(itemPair[0]) : itemPair[0];
    value = decode ? decodeURIComponent(itemPair[1]) : itemPair[1];
    if (name)
      args[name] = value;
  });
  return args;
}

function searchObjArray(array, keyName, keyValue, target) {
  for (var i = 0; i < array.length; i++) {
    if (array[i][keyName] == keyValue) {
      return array[i][target];
    }
  }
  return null;
}

// 数值转化
function numConvert(num, compare, target) {
  compare = compare || -1;
  target = target || null;
  return num === compare ? target : num;
}

// 通用基础Layer
function baseLayer(cb) {
  var _layer = layer.confirm('是否确认此操作？', {
    btn: ['确认', '取消'],
  }, function () {
    layer.close(_layer);
    cb();
  }, function () {
    layer.close(_layer);
  });
}


// 通用currency
function currencyLayer(text, cb, cb2) {
  var _layer = layer.confirm(text, {
    btn: ['确认', '取消'],
  }, function () {
    layer.close(_layer);
    cb();
  }, function () {
    layer.close(_layer);
    cb2();
  });
}

// 失败
function removeLayer() {
  var _layer = layer.confirm('操作失败', {
    btn: ['确认'],
  }, function () {
    layer.close(_layer);
  });
}

// 确认 驳回
function baseLayera(cb) {
  var _layer = layer.confirm('<input type="text"/> ', {
    btn: ['确认', '取消'],
    input: [],
  }, function () {
    layer.close(_layer);
    cb();
  }, function () {
    layer.close(_layer);
  });
}

// 通用多条Layer
function multiLayer(length, cb) {
  if (!length || length === 0) {
    layer.alert('请至少选择一行进行操作');
  } else {
    baseLayer(cb);
  }
}

// 通用formatter, map可以为对象和列表
function commonFormat(map) {
  return function (value, rowData, rowIndex) {
    return map[value];
  };
}

// 默认函数
var defaultFun = function () {
  console.log('exe default');
};

// 拼接目的地址
function jointUrl(url, query) {
  query = query || {};
  var firstFlag = true;
  for (var key in query) {
    if (query.hasOwnProperty(key)) {
      if (firstFlag) {
        url += '?' + key + '=' + query[key];
        firstFlag = false;
      } else {
        url += '&' + key + '=' + query[key];
      }
    }
  }
  return url;
}

// iframe生成Layer
function iframeLayer(funName) {
  if (!window[funName]) {
    window[funName] = function (url) {
      layer.open({
        type: 2,
        shadeClose: true,
        shade: false,
        maxmin: false,
        area: ['100%', '100%'],
        content: url
      });
    };
  }
}

// 图标生成=====================================================================
// 链接类=======================================================================
// 生成通用
function genCommon(title, funName, url) {
  iframeLayer(funName);

  return '<a class="operation-link" style="margin-left: 4px;" href="javascript:' + funName + '(\'' +
    url + '\')"><i class=\'fa fa-thumbs-o-up\'></i> ' + title + '</a>';
}

// 生成查看
function genCheck(url) {
  iframeLayer('_check_new');

  return '<a class="operation-link" style="margin-left: 4px;" href="javascript:_check_new(\'' +
    url + '\')"><i class="fa fa-file-text"></i> 查看</a>';
}

// 生成复核
function genReAudit(url) {
  iframeLayer('_re_audio_new');

  return '<a class="operation-link" style="margin-left: 4px;" href="javascript:_re_audio_new(\'' +
    url + '\')"><i class="fa fa-thumbs-o-up"></i> 复核</a>';
  // return ' <a class="operation-link" style="margin-left: 2px;" href="' + url + '"><i class="fa fa-thumbs-o-up"></i> 审核</a>';
}

// 函数类=======================================================================
// 生成缺考
function genMissexam(proUuids, cb) {
  cb = cb || defaultFun;
  if (!window['_Missexam']) {
    window['_Missexam'] = function (_proUuids) {
      missLayer(function () {
        missExamination({
          processUuid: _proUuids
        }, function (res) {
          if (res.code == 0) {
            cb();
          } else {
            layer.msg(res.msg);
          }
        });
      });
    };
  }
  return '<a class="operation-link" style="margin-left: 4px;" href="javascript:_Missexam(\'' +
    proUuids + '\')"><i class="fa fa-times-circle-o"></i> 缺考</a>';
}

function dateFilter(date, relativeDate, type) { // type暂未使用
  if (!date || !relativeDate) return '-';
  // 20170102
  // var _date = date.substr(0, 4) + '-' + date.substr(4, 2) + '-' +
  //     date.substr(6, 2);
  // return Math.ceil((Date.now() - new Date(_date)) / yearMS);

  var _year = parseInt(date.substr(0, 4));
  var _now = new Date(relativeDate.split(' ')[0]);
  var yearDiff = _now.getFullYear() - _year;
  if (yearDiff > 1) return yearDiff;
  return '1';
}

function getQueryString(name) {
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]);
  return null;
};

function dateFilter(date, relativeDate, type) { // type暂未使用
  if (!date || !relativeDate) return '-';
  // 20170102
  // var _date = date.substr(0, 4) + '-' + date.substr(4, 2) + '-' +
  //     date.substr(6, 2);
  // return Math.ceil((Date.now() - new Date(_date)) / yearMS);

  var _year = parseInt(date.substr(0, 4));
  var _now = new Date(relativeDate.split(' ')[0]);
  var yearDiff = _now.getFullYear() - _year;
  if (yearDiff > 1) return yearDiff;
  return '1';
}

function accessStorage(name, defaultValue) {
  return sessionStorage.getItem(name) || defaultValue;
}
